package cn.yunhe.controller;

import cn.yunhe.service.QuestionService;
import cn.yunhe.util.ListToQuestionUtil;
import cn.yunhe.util.MultipartFileToFile;
import cn.yunhe.util.ReadExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.view.json.MappingJackson2JsonView;


import java.io.IOException;
import java.util.List;

@Controller
public class QuestionController {

    @Autowired
    QuestionService questionService;
    /**
     * test
     */
    @RequestMapping(value="/question/{ftlUrl}",method = RequestMethod.GET)
    public String toQtlist(@PathVariable("ftlUrl") String ftlUrl){
        return ftlUrl;
    }

    @RequestMapping("/fileUpload")
    public ModelAndView fileUpload(@RequestParam("fileName") MultipartFile file) throws IOException {
        ModelAndView mv=new ModelAndView();

        if(file.isEmpty()){
            mv.addObject("flag","没成功");
            mv.setView(new MappingJackson2JsonView());

            return mv;
        }
        List<List<String>> lists=ReadExcel.readExcel(MultipartFileToFile.translate(file));
        for (List<String> list:lists){

           questionService.addQuestion(ListToQuestionUtil.getQuestion(list));
        }
        mv.addObject("lists",lists);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }


}
